package cn.hotel.service;

import cn.hotel.dao.AdministratorDao;
import cn.hotel.pojo.Administrator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdministratorServiceImpl implements AdministratorService {
    cn.hotel.dao.Administrator administrator = new AdministratorDao();

    //查询管理员
    @Override
    public Map<String, Object> administratorMap(String administratorName) {
        Map<String ,Object> objectMap = new HashMap<>();
        try {
            List<Administrator> administrators = administrator.administratorList(administratorName);
            if(administrators != null && administrators.size() > 0){
                objectMap.put("flag","1");
                objectMap.put("mag","查询成功");
                objectMap.put("data",administrators);
            }else {
                objectMap.put("flag","0");
                objectMap.put("mag","查询失败");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            objectMap.put("flag","-1");
            objectMap.put("mag","程序异常");
        }
        return objectMap;
    }

    //添加管理员
    @Override
    public Map<String, Object> addAdministratorMap(cn.hotel.pojo.Administrator administrators) {
        Map<String ,Object> objectMap = new HashMap<>();
        try {
            int num = administrator.addAdministrator(administrators);
            if(num > 0){
                objectMap.put("flag","1");
                objectMap.put("mag","添加成功");
                objectMap.put("data",num);
            }else {
                objectMap.put("flag","0");
                objectMap.put("mag","添加失败");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            objectMap.put("flag","-1");
            objectMap.put("mag","程序异常");
        }
        return objectMap;
    }

    //删除管理员
    @Override
    public Map<String, Object> deleteAdministratorMap(String name) {
        Map<String ,Object> objectMap = new HashMap<>();
        try {
            int num = administrator.deleteAdministrator(name);
            if(num > 0){
                objectMap.put("flag","1");
                objectMap.put("mag","删除成功");
                objectMap.put("data",num);
            }else {
                objectMap.put("flag","0");
                objectMap.put("mag","删除失败");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            objectMap.put("flag","-1");
            objectMap.put("mag","程序异常");
        }
        return objectMap;
    }

    //修改管理员权限
    @Override
    public Map<String, Object> changeMap(cn.hotel.pojo.Administrator administrators) {
        Map<String ,Object> objectMap = new HashMap<>();
        try {
            int num = administrator.change(administrators);
            if(num > 0){
                objectMap.put("flag","1");
                objectMap.put("mag","修改成功");
                objectMap.put("data",num);
            }else {
                objectMap.put("flag","0");
                objectMap.put("mag","修改失败");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            objectMap.put("flag","-1");
            objectMap.put("mag","程序异常");
        }
        return objectMap;
    }
}
